/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Supporter.OurDialog;
import Supporter.OurLogger;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;

/**
 *
 * @author Ivan
 */
public class OurListener implements Runnable {

    private int port;

    public OurListener(int p) {
        this.port = p;
    }

    @Override
    public void run(){
        try {
            ServerSocket listener = new ServerSocket(port);
            OurLogger.logNotError(this, "Listen to clients on port " + port, Level.INFO);

            while (true) {
                Socket requester = listener.accept();
                OurLogger.logNotError(this, "Receive Client Connection from PC: " + requester.getLocalAddress().getHostName(), Level.INFO);
                new Thread(new ClientHandler(requester, this)).start();                
            }
        } catch (IOException ex) {
            OurLogger.logError(this, "run()", ex, Level.SEVERE);
            OurDialog.showErrorDialog("Không thể mở port " + port + "! Hãy chọn port khác.");
        }
    }
}
